<template>
  <view class="webview-container">
    <web-view :src="url" @error="urlLoadError"></web-view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const url = ref('');

onMounted(() => {
  // 获取页面参数中的url
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const options = currentPage.options || currentPage.$page.options;
  if (options.url && options.url.startsWith('http')) {
    url.value = decodeURIComponent(url.value);
  } else {
    uni.showToast({
      title: '无效的链接',
      icon: 'none'
    });
    setTimeout(() => {
      uni.navigateBack();
    }, 1500);
  }
});

const urlLoadError = () => {
	uni.showToast({
	  title: '网页链接错误！',
	  icon: 'none'
	});
	setTimeout(() => {
	  uni.navigateBack();
	}, 2000);
}
</script>

<style lang="scss" scoped>
.webview-container {
  width: 100%;
  height: 100vh;
}
</style> 